package com.shs.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shs.sys.common.Constants;
import com.shs.sys.common.DataGridView;
import com.shs.sys.common.ResultObj;
import com.shs.sys.common.TreeNode;
import com.shs.sys.entity.Permission;
import com.shs.sys.service.PermissionService;
import com.shs.sys.vo.PermissionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.Action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jxm
 * @since 2020-04-09
 */
@RestController
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /****************权限管理开始****************/

    /**
     * 加载权限管理左边的权限树json
     * @param permissionVo
     * @return
     */
    @RequestMapping("loadPermissionManagerLeftTreeJson")
    public DataGridView loadPermissionManagerLeftTreeJson(PermissionVo permissionVo){
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",Constants.TYPE_MENU);
        List<Permission> permissions = permissionService.list(queryWrapper);
        List<TreeNode> treeNodes = new ArrayList<>();
        for (Permission permission : permissions) {
            Boolean spread = permission.getOpen()==1?true:false;
            treeNodes.add(new TreeNode(permission.getId(),permission.getPid(),permission.getTitle(),spread));
        }
        return new DataGridView(treeNodes);
    }

    /**
     * 查询
     */
    @RequestMapping("loadAllPermission")
    public DataGridView loadAllPermission(PermissionVo vo){
        Page<Permission> page = new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<Permission> wrapper = new QueryWrapper();
        wrapper.eq("type",Constants.TYPE_PERMISSION);
        wrapper.like(StringUtils.isNotBlank(vo.getTitle()),"title",vo.getTitle());
        wrapper.like(StringUtils.isNotBlank(vo.getPercode()),"percode",vo.getPercode());
        wrapper.eq(vo.getId()!=null,"pid",vo.getId());
        wrapper.orderByAsc("ordernum");
        permissionService.page(page,wrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 获取权限排序码
     */
    @RequestMapping("loadPermissionMaxOrderNum")
    public Map<String,Object> loadPermissionMaxOrderNum(){
        Map<String,Object> result = new HashMap<>();
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",Constants.TYPE_PERMISSION);
        queryWrapper.orderByDesc("ordernum");
        List<Permission> permissions = permissionService.list(queryWrapper);
        if(null!=permissions && permissions.size()>0){
            result.put("value",permissions.get(0).getOrdernum()+1);
        }else{
            result.put("value",1);
        }
        return result;
    }

    /**
     * 添加权限
     */
    @RequestMapping("addPermission")
    public ResultObj addPermission(PermissionVo permissionVo){
        try {
            permissionVo.setType(Constants.TYPE_PERMISSION);//设置添加类型
            permissionService.save(permissionVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改权限
     */
    @RequestMapping("updatePermission")
    public ResultObj updatePermission(PermissionVo permissionVo){
        try {
            permissionService.updateById(permissionVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     */
    @RequestMapping("deletePermission")
    public ResultObj deletePermission(PermissionVo vo){
        try {
            permissionService.removeById(vo.getId());
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /****************权限管理结束****************/

}
